/**
 *
 * Exercise 2.6 -  Write a function setbits(x,p,n,y) that returns x with the
 * n bits that begin at positionp set to the rightmost n bits of y,leaving
 * the other bits unchanged.
 **/

#include <stdio.h>

unsigned setbits(unsigned x,int p,int n,unsigned y);

int main(void)
{
    printf("%u\n",setbits(722U,4,3,89U));
}

unsigned setbits(unsigned x,int p,int n,unsigned y)
{
    return ( x & ~(~(~0<<n)<<(p+1-n)) ) | ( (y<<(p+1-n) & (~(~0<<n)<<(p+1-n))) );
}